package com.photoeditor.demo.ui.dialog;

import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.common.base.ComponentContext;
import com.common.base.utils.DeviceUtils;
import com.photoeditor.R;
import com.photoeditor.demo.statistics.StatisticsConstant;
import com.photoeditor.demo.statistics.StatisticsUtils;
import com.photoeditor.demo.ui.widget.CenterAlignImageSpan;
import com.photoeditor.demo.util.FontUtil;

/**
 * Created by winn on 17/6/13.
 */

public class UpdateDialog extends NormalDialog {

    public UpdateDialog(Context context) {
        this(context, R.style.dialog_style);
    }

    public UpdateDialog(Context context, int themeResId) {
        super(context, themeResId);
        initView();
    }

    protected UpdateDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        initView();
    }

    private void initView() {
        setContentView(R.layout.layout_filter_version_low_dialog);
        mTitle = (TextView) findViewById(R.id.title);
        mContent = (TextView) findViewById(R.id.message);
        mPositiveButton = (Button) findViewById(R.id.ok);

        setPositiveBtn(getContext().getResources().getString(R.string.emoji_download),
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String appPackageName = getContext().getPackageName();
                        if (TextUtils.isEmpty(appPackageName)) {
                            appPackageName = ComponentContext.getPackageName();
                        }
                        try {
                            Intent launchIntent = getContext().getPackageManager().getLaunchIntentForPackage("com.android.vending");
                            ComponentName comp = new ComponentName("com.android.vending", "com.google.android.finsky.activities.LaunchUrlHandlerActivity");
                            launchIntent.setComponent(comp);
                            launchIntent.setData(Uri.parse("market://details?id=" + appPackageName));
                            getContext().startActivity(launchIntent);
                        } catch (Exception ex) {
                            try {
                                getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        StatisticsUtils.statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_FILTER_UPDATE_DIALOG,
                                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_ACTION, StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_FILTER_UPDATE_DIALOG_DOWNLOAD);
                        dismiss();
                    }
                });

        Drawable titleDrawable = getContext().getResources().getDrawable(R.drawable.ic_update_emoji1);
        titleDrawable.setBounds(0, 0, titleDrawable.getIntrinsicWidth(), titleDrawable.getIntrinsicHeight());
        mTitle.setCompoundDrawables(null, titleDrawable, null, null);
//        mTitle.setCompoundDrawablePadding(DeviceUtils.dip2px(getContext(), 5));
        mTitle.setVisibility(View.VISIBLE);

        String str = getContext().getResources().getString(R.string.filter_version_low_tip);
        SpannableString sp = new SpannableString(str);

        Drawable drawable = getContext().getResources().getDrawable(R.drawable.ic_update_emoji2);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());

        //居中对齐imageSpan
        CenterAlignImageSpan imageSpan = new CenterAlignImageSpan(drawable);
        int start = str.indexOf("1");
        sp.setSpan(imageSpan, start, start + 1, ImageSpan.ALIGN_BASELINE);

        drawable = getContext().getResources().getDrawable(R.drawable.ic_update_emoji3);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        CenterAlignImageSpan imageSpan2 = new CenterAlignImageSpan(drawable);
        int start2 = str.indexOf("2");
        sp.setSpan(imageSpan2, start2, start2 + 1, ImageSpan.ALIGN_BASELINE);
        CenterAlignImageSpan imageSpan3 = new CenterAlignImageSpan(drawable);
        int start3 = str.indexOf("3");
        sp.setSpan(imageSpan3, start3, start3 + 1, ImageSpan.ALIGN_BASELINE);
        CenterAlignImageSpan imageSpan4 = new CenterAlignImageSpan(drawable);
        int start4 = str.indexOf("4");
        sp.setSpan(imageSpan4, start4, start4 + 1, ImageSpan.ALIGN_BASELINE);

        mContent.setVisibility(View.VISIBLE);
        mContent.setLineSpacing(DeviceUtils.dip2px(getContext(), 8), 1);
        mContent.setText(sp);
        FontUtil.setCustomFont(mContent);
        FontUtil.setCustomFontBold(mPositiveButton);

        setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                StatisticsUtils.statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_FILTER_UPDATE_DIALOG,
                        StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_ACTION, StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_FILTER_UPDATE_DIALOG_CANCEL);
            }
        });
    }

    @Override
    public void show() {
        super.show();
        StatisticsUtils.statisicsCustomFunction(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_FILTER_UPDATE_DIALOG,
                StatisticsConstant.UMENG_CUSTOM_EVENT_KEY_ACTION, StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_FILTER_UPDATE_DIALOG_VALUE_DISPLAY);
    }
}
